/******************************************************************************
    Copyright (c) 2006 by RockOS.
    All rights reserved.

    This software is supported by the Rock Software Workroom only.

    Any bugs please contact the author with e-mail or QQ:
     E-mail : baobaoba520@yahoo.com.cn
         QQ : 59681888
*******************************************************************************
File name   : music.c
Description : play music with ITSN44b0x develop board.
            : 
            : 
Auther      : sunxinqiu
History     :
  2006-03-15   first release.
******************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "..\inc\typedef.h"
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\music.h"

MUSIC_NOTE defaultMusic[] = 
{
/* note       duration */
/* ========   =========*/
    {NOTE_D1, 50},
    {NOTE_D2, 50},
    {NOTE_D3, 50},
    {NOTE_D4, 50},
    {NOTE_D5, 50},
    {NOTE_D6, 50},
    {NOTE_D7, 50},
    {NOTE_Z1, 50},
    {NOTE_Z2, 50},
    {NOTE_Z3, 50},
    {NOTE_Z4, 50},
    {NOTE_Z5, 50},
    {NOTE_Z6, 50},
    {NOTE_Z7, 50},
    {NOTE_G1, 50},
    {NOTE_G2, 50},
    {NOTE_G3, 50},
    {NOTE_G4, 50},
    {NOTE_G5, 50},
    {NOTE_G6, 50},
    {NOTE_G7, 50},
    {NOTE_G6, 50},
    {NOTE_G5, 50},
    {NOTE_G4, 50},
    {NOTE_G3, 50},
    {NOTE_G2, 50},
    {NOTE_G1, 50},
    {NOTE_Z7, 50},
    {NOTE_Z6, 50},
    {NOTE_Z5, 50},
    {NOTE_Z4, 50},
    {NOTE_Z3, 50},
    {NOTE_Z2, 50},
    {NOTE_Z1, 50},
    {NOTE_D7, 50},
    {NOTE_D6, 50},
    {NOTE_D5, 50},
    {NOTE_D4, 50},
    {NOTE_D3, 50},
    {NOTE_D2, 50},
    {NOTE_D1, 50},
    {0,0}
};

/******************************************************************************
Function    : void play_note(int note)
Params      : note - the music note, since NOTE_0 to NOTE_G7
            : 
            : 
            : 
Return      : N/A
Description : plat a single music note.
            : To run this function normally, the Tick frequance must be 100Hz,
            : i.e. 100Ticks per second.
******************************************************************************/
void play_note(int note)
{
    unsigned int tout;

    switch(note)
    {
    case NOTE_0:
        tout = FREQ_MUTE;
        break;
    case NOTE_D1:
        tout = 100000/FREQ_D1;
        break;
    case NOTE_D2:
        tout = 100000/FREQ_D2;
        break;
    case NOTE_D3:
        tout = 100000/FREQ_D3;
        break;
    case NOTE_D4:
        tout = 100000/FREQ_D4;
        break;
    case NOTE_D5:
        tout = 100000/FREQ_D5;
        break;
    case NOTE_D6:
        tout = 100000/FREQ_D6;
        break;
    case NOTE_D7:
        tout = 100000/FREQ_D7;
        break;
    case NOTE_Z1:
        tout = 100000/FREQ_Z1;
        break;
    case NOTE_Z2:
        tout = 100000/FREQ_Z2;
        break;
    case NOTE_Z3:
        tout = 100000/FREQ_Z3;
        break;
    case NOTE_Z4:
        tout = 100000/FREQ_Z4;
        break;
    case NOTE_Z5:
        tout = 100000/FREQ_Z5;
        break;
    case NOTE_Z6:
        tout = 100000/FREQ_Z6;
        break;
    case NOTE_Z7:
        tout = 100000/FREQ_Z7;
        break;
    case NOTE_G1:
        tout = 100000/FREQ_G1;
        break;
    case NOTE_G2:
        tout = 100000/FREQ_G2;
        break;
    case NOTE_G3:
        tout = 100000/FREQ_G3;
        break;
    case NOTE_G4:
        tout = 100000/FREQ_G4;
        break;
    case NOTE_G5:
        tout = 100000/FREQ_G5;
        break;
    case NOTE_G6:
        tout = 100000/FREQ_G6;
        break;
    case NOTE_G7:
        tout = 100000/FREQ_G7;
        break;
    default:
        tout = FREQ_MUTE;
        break;
    }
    if (tout != FREQ_MUTE)
    {
        pwm_start(PWM_TIMER2, tout, 1);
    }
    else
    {
        pwm_stop(PWM_TIMER2);
    }
}

/******************************************************************************
Function    : void play_music (MUSIC_NOTE * pMusic)
Params      : pMusic - the music notes sequance which combineing a music.
            : 
            : 
            : 
Return      : N/A
Description : To run this function normally, the Tick frequance must be 100Hz,
            : i.e. 100 Ticks per second.
******************************************************************************/
void play_music (MUSIC_NOTE * pMusic)
{
    MUSIC_NOTE * p;

    if (pMusic == NULL)
    {
        //OS_printf("play_music(): no valid music notes sequance...\n");
        return;
    }

    p = pMusic;
    while(p->duration != 0)
    {
        play_note(p->note);
        //taskDelay(p->duration);
        delay(p->duration * 50000); 
        p++;
    }

    play_note(NOTE_0);
}

